package com.motu.monstercity.server.gate;

import com.motu.monstercity.module.common.utility.EventBusConstant;
import com.motu.vertx.module.utility.base.ServerConfig;
import com.motu.vertx.module.utility.monitor.GateMonitorManager;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * monitor_gate数据更新
 */
public class GateMonitorVerticle extends AbstractVerticle {
    public final static Logger logger = LoggerFactory.getLogger(GateMonitorVerticle.class);

    @Override
    public void start(Promise<Void> future) throws Exception {

        EventBus eb = vertx.eventBus();

        String ebName = EventBusConstant.getAddress(EventBusConstant.MONITOR_SAVEDATA);
        MessageConsumer<JsonObject> consumer = eb.consumer(ebName);
        logger.info("---===#*#*#*#===---* gate start  ebName:" + ebName);
        consumer.handler(message -> {

            JsonObject recData = message.body();
            int time = recData.getInteger("time");
            ConcurrentHashMap<Long, ServerWebSocket> conMap = SocketManager.getSocketList();
            logger.info("---===#*#*#*#===---* gate receive notice save monitor_gate time:" + time + " conMap.size = " + conMap.size());

            String sql2 = "insert into monitor_gate(gate_name,socket_num,package_num,request_data_size,response_data_size,deal_time,init_package_num,init_response_data_size,init_deal_time,add_time,gate_queue_num,timeout_num) values(?,?,?,?,?,?,?,?,?,?,?,?);";
            JsonArray params2 = new JsonArray();
            params2.add(ServerConfig.getGateName());
            params2.add(conMap.size());
            params2.add(GateMonitorManager.getPackageNum());
            params2.add(GateMonitorManager.getRequestDataSize());
            params2.add(GateMonitorManager.getResponseDataSize());
            params2.add(GateMonitorManager.getDealTime());
            params2.add(GateMonitorManager.getInitPackageNum());
            params2.add(GateMonitorManager.getInitResponseDataSize());
            params2.add(GateMonitorManager.getInitDealTime());
            params2.add(time);
            params2.add(SocketManager.getTotalMessageQueueNum());
            params2.add(GateMonitorManager.getTimeoutNum());
            GateMonitorManager.resetAll();//立即清除数据
            MysqlTool.updateToDb(MysqlClient.getServerPool(), sql2, params2, handler -> {

                if (handler.failed()) {
                    logger.error(handler.cause().getMessage());
                }
            });

        });

        consumer.completionHandler(res2 -> {
            if (res2.succeeded()) {
                future.complete();
                logger.info("The Save(monitor_gate)Verticle handler registration has reached all nodes");
            } else {
                future.fail("The Save(monitor_gate)Verticle handler registration has failed");
                logger.error("Save(monitor_gate)Verticle Registration failed! " + res2.cause().toString());
            }
        });

    }


}
